package com.itdev.matcherapp.android;

import java.util.ArrayList;
import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentReceiver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.itdev.matcherapp.android.fakeservice.PeopleLocationBean;

public class PeopleMatchingList extends ExpandableListActivity {
	public static final String ACTION_MATCHING_LIST_CHANGE = "com.itdev.matcherapp.android.ACTION_MATCHING_LIST_CHANGE";
	private IntentFilter intentFilter = new IntentFilter(Map.ACTION_LOCATION_CHANGE);
	private IntentReceiver intentReceiver = new IntentReceiverMatchingListChange();
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		registerReceiver(intentReceiver, intentFilter);
	}
	
	public void setPeopleList(List<PeopleLocationBean> peopleList){
		setListAdapter(new PeopleMatchingListAdapter(this, peopleList));
	}

	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(intentReceiver);	
	}
}

class PeopleMatchingListAdapter extends BaseExpandableListAdapter{
	private List<PeopleLocationBean> peopleList = new ArrayList<PeopleLocationBean>();
	private Context context;
	public PeopleMatchingListAdapter(Context context, List<PeopleLocationBean> peopleList){
		this.context = context;
		this.peopleList = peopleList;
	}
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Object getGroup(int groupPosition) {
		return peopleList.get(groupPosition);
	}

	public int getGroupCount() {
		return peopleList.size();
	}

	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		PeopleLocationBean people = (PeopleLocationBean) getGroup(groupPosition);
		TextView textView = new TextView(context);
		textView.setText(people.getGmailAccount());
		textView.setPadding(45, 0, 0, 0);	
		return textView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean stableIds() {
		// TODO Auto-generated method stub
		return false;
	}
	
}

class IntentReceiverMatchingListChange extends IntentReceiver{

	@Override
	public void onReceiveIntent(Context context, Intent intent) {
		List<PeopleLocationBean> peopleList = (List<PeopleLocationBean>) intent.getSerializableExtra("peoplelist");
		PeopleMatchingList peopleMatching = (PeopleMatchingList) context;
		peopleMatching.setPeopleList(peopleList);
	}
	
}